package com.yy.wga.income.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.yy.wga.common.web.intereceptor.RequestHolder;
import com.yy.wga.income.feign.request.IncomeOrderListRequest;
import com.yy.wga.income.feign.response.IncomeOrderListResponse;
import com.yy.wga.income.service.mapper.IncomeOrderGoodsMapper;
import com.yy.wga.income.service.pojo.IncomeOrderGoodsDO;
import com.yy.wga.income.service.pojo.IncomeUserBalanceDO;
import com.yy.wga.income.service.service.IncomeOrderGoodsService;
import com.yy.wga.income.service.service.IncomeUserBalanceService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 订单收益(IncomeOrder)表服务实现类
 *
 * @author guochao
 * @since 2024-11-12 00:27:45
 */
@Service
@RequiredArgsConstructor
public class IncomeOrderGoodsServiceImpl extends ServiceImpl<IncomeOrderGoodsMapper, IncomeOrderGoodsDO> implements IncomeOrderGoodsService {
    private final IncomeUserBalanceService userBalanceService;

    @Override
    public List<IncomeOrderListResponse> pageIncome(IncomeOrderListRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), false);
        List<IncomeOrderGoodsDO> incomeOrderList = baseMapper.selectList(new LambdaQueryWrapper<IncomeOrderGoodsDO>()
                .eq(IncomeOrderGoodsDO::getAppKey, RequestHolder.getBaseRequest().getAppKey())
                .eq(IncomeOrderGoodsDO::getUserId, request.getUserId())
                .eq(IncomeOrderGoodsDO::getType, request.getType())
                .gt(IncomeOrderGoodsDO::getCommissionValue, 0)
                .eq(request.getStatus() != null, IncomeOrderGoodsDO::getStatus, request.getStatus())
                .orderByDesc(IncomeOrderGoodsDO::getId));
        if (CollectionUtils.isEmpty(incomeOrderList)) {
            return Collections.emptyList();
        }
        return com.yy.wga.common.utils.mapping.BeansUtils.mapList(incomeOrderList, IncomeOrderListResponse.class);
    }

    @Override
    @Transactional
    public void orderSettle(String appKey, String orderNo) {
        PageHelper.startPage(1, 10, false);
        List<IncomeOrderGoodsDO> incomeOrderList = baseMapper.selectList(new LambdaQueryWrapper<IncomeOrderGoodsDO>()
                .eq(IncomeOrderGoodsDO::getAppKey, appKey)
                .eq(IncomeOrderGoodsDO::getOrderNo, orderNo));
        if (incomeOrderList == null || incomeOrderList.isEmpty()) {
            return;
        }
        for (IncomeOrderGoodsDO incomeOrderGoodsDO : incomeOrderList) {
            IncomeOrderGoodsDO update = new IncomeOrderGoodsDO();
            update.setId(incomeOrderGoodsDO.getId());
            update.setStatus(2);
            update.setSettleTime(new Date());
            baseMapper.updateById(update);

            // 发放收益
            IncomeUserBalanceDO updateBalance = new IncomeUserBalanceDO();
            updateBalance.setUserId(incomeOrderGoodsDO.getUserId());
            updateBalance.setUnSettle(-incomeOrderGoodsDO.getCommissionValue());
            updateBalance.setSettle(incomeOrderGoodsDO.getCommissionValue());
            userBalanceService.incrementIncome(updateBalance);
        }
    }
}
